package com.fortune.service.impl;

import com.fortune.constants.enums.MStore;
import com.fortune.constants.enums.ProductStock;
import com.fortune.dao.DAccountInfoDao;
import com.fortune.dao.MProductStockDetailInfoDao;
import com.fortune.dao.MStoreOutDao;
import com.fortune.dto.in.mStoreOut.MStoreOutListDto;
import com.fortune.service.MStoreOutService;
import com.fortune.vo.mStoreOut.MStoreOutInfo;
import com.fortune.vo.mStoreOut.MStoreOutInfoDetail;
import com.fortune.vo.mStoreOut.MStoreOutInfoDetailIn;
import com.fortune.vo.mStoreOut.ProduceDetailIn;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class MStoreOutServiceImpl implements MStoreOutService {
    @Autowired
    private MStoreOutDao mStoreOutDao;
    @Autowired
    private DAccountInfoDao dAccountInfoDao;
    @Autowired
    private MProductStockDetailInfoDao mProductStockDetailInfoDao;

    /**
     * 查询出库单列表
     * @param companyId
     * @param mStoreOutListDto
     * @return
     */
    @Override
    public List<MStoreOutInfo> queryMStoreOutList(String companyId, MStoreOutListDto mStoreOutListDto) {
        return mStoreOutDao.queryMStoreOutList(companyId,mStoreOutListDto);
    }

    /**
     * 查询出库单列表个数
     * @param companyId
     * @param mStoreOutListDto
     * @return
     */
    @Override
    public int queryMStoreOutListCount(String companyId, MStoreOutListDto mStoreOutListDto) {
        return mStoreOutDao.queryMStoreOutListCount(companyId,mStoreOutListDto);
    }

    /**
     * 出库单详情
     * @param companyId
     * @param id
     * @return
     */
    @Override
    public MStoreOutInfoDetail queryMStoreOutInfoDetail(String companyId,String id) {
        return mStoreOutDao.queryMStoreOutInfoDetail(companyId,id);
    }

    /**
     * 用于经销商入库的出库单详情
     * @param companyId
     * @param id
     * @return
     */
    @Override
    public MStoreOutInfoDetailIn queryMStoreOutInfoDetailIn(String companyId, String id) {
        return mStoreOutDao.queryMStoreOutInfoDetailIn(companyId,id);
    }

    /**
     * 确定发货
     * @param companyId
     * @param detail
     * @return
     */
    @Override
    @Transactional
    public int updateMStoreOutShipType(String userId,String companyId, MStoreOutInfoDetailIn detail,String id) {
        //入库单ID
        String inId = UUID.randomUUID().toString().replace("-","");
        int num = mStoreOutDao.updateType(userId,companyId,id);
        if (num < 1){
            throw new RuntimeException("确定发货失败");
        }
        String dealerId = detail.getDealerId();
        Integer inNum = dAccountInfoDao.queryDInBaseInfoNo(dealerId);
        String endNum = "RK000001";
        if (inNum!= null){
            StringBuffer string = new StringBuffer();
            if (inNum.toString().length()!=6){
                for (int i = 0; i < 6-inNum.toString().length();i++){
                    string.append("0");
                }
            }
            endNum = "RK" + string.toString() + (inNum+1);
        }
        num = dAccountInfoDao.addStoreOutAccount(inId,detail, MStore.NO, endNum);
        if (num < 1){
            throw new RuntimeException("确定发货失败");
        }
        num = dAccountInfoDao.addStoreOutAccountInfo(inId,companyId,dealerId,detail.getProduce());
        if (num < 1){
            throw new RuntimeException("确定发货失败");
        }
        //发货仓库ID
        String shipSoreId = detail.getShipSore();
        List<ProduceDetailIn> list = detail.getProduce();
        for (int i = 0; i < list.size(); i++){
            ProduceDetailIn produceDetailOut = list.get(i);
            int order = mProductStockDetailInfoDao.queryMax(companyId,shipSoreId,produceDetailOut.getMpid());
            List<ProductStockDetailInfo> lastDate = mProductStockDetailInfoDao.queryLastOutStockDetail(companyId,shipSoreId,produceDetailOut.getMpid());
            if (lastDate == null || lastDate.size() == 0){
                throw new RuntimeException("确定发货失败,请查看是否有库存");
            }
            int remainNum = lastDate.get(0).getRemainNum() - produceDetailOut.getOrderNum();

            num = mProductStockDetailInfoDao.insertSingleOutProductStockDetail(companyId,shipSoreId, ProductStock.XSCK,produceDetailOut,order,remainNum);
            if (num < 1){
                throw new RuntimeException("验收入库失败");
            }
        }

        return 1;
    }

}
